package by.grsu.na.page.sample;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.na.app.MyWebApplication;
import by.grsu.na.dao.impl.BlackClientDao;
import by.grsu.na.dao.impl.ClientDao;
import by.grsu.na.entity.BlackClient;
import by.grsu.na.entity.Client;




public class ClientsListPage extends WebPage {

	public ClientsListPage() {
		super();
		
		
		
		
		

		final ClientDao clientDao = new ClientDao(MyWebApplication.DATA_XML_FOLDER);
		final BlackClientDao blackClientDao = new BlackClientDao(MyWebApplication.DATA_XML_FOLDER);

		final List<Client> allClients = clientDao.getAll(); 
		final List<BlackClient> allBlackClients = blackClientDao.getAll(); 

		add(new DataView<Client>("table-rows", new ListDataProvider<Client>(allClients)) {
			@Override
			protected void populateItem(final Item<Client> item) {
				final Client client = item.getModelObject();
				item.add(new Label("id", client.getId()));
				item.add(new Label("clientName", client.getClientName()));
				item.add(new Label("clientLogin", client.getClientLogin()));
				item.add(new Label("clientPasword", client.getClientPasword()));
				item.add(new Label("clientBlackEdit", client.getClientBlackEdit()));
				
				
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new ClientEditPage(client));
					}
				});
				
				item.add(new Link("blackLink") {
					@Override
					public void onClick() {
						setResponsePage(new ClientBlackPage(client));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {

						clientDao.delete(client.getId());
						setResponsePage(new ClientsListPage());
						
					}
				});
			}
		});

		add(new Link("createLink") {
			
			@Override
			public void onClick() {
				
				setResponsePage(new ClientEditPage(new Client()));
			}
		});
		
		
		add(new Link("createBlackLink") {
			
			@Override
			public void onClick() {
				
				setResponsePage(new BlackClientsListPage()); 
			}
		});
		
		
		
		
	}
}